package com.example.user_manage.Component;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 自定义访问失败处理器
 */
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {

    /**
     * 处理方法
     * @param request 请求对象
     * @param response 响应对象
     * @param accessDeniedException 访问拒绝异常队形
     * @throws IOException IO异常
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException{
        //指定响应的字符集编码
        response.setContentType("application/json;charset=utf-8 charset=utf-8");
        response.getWriter().write("你无权访问该地址，奋斗吧，少年！");
    }
}
